import logging

import pytest

from resource.pom.gallery import Gallery
from resource.pom.keyboard import Keyboard
from resource.pom.memorandum import Memorandum
from utils.manager import Images


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：预置图片资源------')
    app = Gallery
    device.mediatool_delete_all()
    device.hdc_file_send(app.photo1, app.photo_dir)
    device.mediatool_send(app.photo_dir)
    yield
    logging.info('------用例后置：删除预置资源------')
    device.mediatool_delete_all()


class Test:
    app = Memorandum
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面-备忘录应用，点击右上角“+”新建笔记')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.add_note)
        logging.info('预期结果1.进入笔记编辑界面')
        device.refresh_layout()
        device.assert_text_exist('标题')
        device.assert_type_exist('Web')
        title = device.get_element_by_text('标题')
        web = device.get_element_by_type('Web')
        classify = device.get_element_by_text('未分类')

        logging.info('步骤2.输入标题内容')
        device.click_element(title)
        device.refresh_layout()
        title_editor = device.get_element_by_condition(condition={'type': 'TextInput', 'hint': '名称'})
        device.click_element(title_editor)
        logging.info('预期结果2.唤醒键盘可以正常输入标题')
        device.assert_soft_keyboard_on()
        device.click(*self.keyboard.key_a)
        device.click(*self.keyboard.key_b)
        device.click(*self.keyboard.key_c)
        device.click(*self.keyboard.close)
        device.click_element(device.get_element_by_text('保存'))
        device.refresh_layout()
        device.assert_text_exist('abc')

        logging.info('步骤3.点击内容位置输入笔记内容')
        device.click_element(web)
        logging.info('预期结果3.唤醒键盘可以正常输入笔记内容')
        device.assert_soft_keyboard_on()
        device.input_text(40, 250, 'ABCDEFG123')
        device.click(*self.keyboard.close)

        logging.info('步骤4.点击清单')
        device.refresh_layout()
        laundry_x, laundry_y = device.center_of_element(device.get_element_by_text('清单'))

        device.click(laundry_x, laundry_y - 50)
        logging.info('预期结果4.可以正常添加清单〇标识')
        device.refresh_layout()
        device.assert_type_exist('checkBox')

        logging.info('步骤5.点击样式')
        device.long_click(130, 245)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('全选'))
        sx, sy = device.center_of_element(device.get_element_by_text('样式'))
        device.click(sx, sy - 50)
        device.refresh_layout()
        device.assert_type_exist('Dialog')
        device.assert_type_exist('Button')
        device.assert_type_exist('Circle')
        device.assert_type_exist('Slider')
        logging.info('依次点击字体加粗，斜体，下划线，居中，编号，蓝色字体，字体放到最大')
        device.click(140, 935)
        device.click(230, 935)
        device.click(316, 935)
        device.click(490, 935)
        device.click(318, 1010)
        device.click(360, 1082)
        device.swipe(210, 1150, 520, 1150)
        # 关掉弹窗
        device.click(360, 640)
        logging.info('预期结果5.输入的内容根据样式的选择显示')
        pic = device.save_snapshot_to_local('edited.jpeg')
        Images.crop_picture(pic, 50, 250, 650, 430)
        similarity = Images.compare_image_similarity(self.app.style_pic, pic)
        assert similarity > 0.8, '样式未生效'

        logging.info('步骤6.点击相册')
        gallery = device.get_element_by_text('相册')
        device.click_element(gallery)
        logging.info('预期结果6.首次点击相册有赋权弹窗，允许后选择已有的图片或点击拍照添加图片添加入笔记中，显示正常')
        device.wait(1)
        device.save_snapshot_to_local('clicked_gallery.jpeg')
        device.refresh_layout()
        device.assert_text_exist('禁止')
        device.assert_text_exist('允许')
        device.click_element(device.get_element_by_text('允许'))
        device.wait(1)
        device.assert_current_window(Gallery.window_name)
        # 选择图片
        device.click(560, 260)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('完成'))
        device.close_soft_keyboard()

        pic2 = device.save_snapshot_to_local('add_pic.jpeg')
        Images.crop_picture(pic2)
        similarity = Images.compare_image_similarity(self.app.add_pic, pic2)
        assert similarity > 0.8, '样式未生效'

        logging.info('步骤7.点击右上角的撤回键和前进键')
        # 点击笔记内容起始位置
        device.click(*self.app.chehui)
        device.wait(1)
        pic3 = device.save_snapshot_to_local('chehui_pic.jpeg')
        Images.crop_picture(pic3)
        device.click(*self.app.qianjin)
        device.wait(1)
        pic4 = device.save_snapshot_to_local('qianjin_pic.jpeg')
        Images.crop_picture(pic4)
        logging.info('预期结果7.撤回键可以返回至上一步，前进键可以根据返回到上一步后再回到本步')
        s23 = Images.compare_image_similarity(pic2, pic3)
        s34 = Images.compare_image_similarity(pic3, pic4)
        assert s23 < 0.8, '撤回图片未生效'
        assert s34 < 0.8, '前进键没有返回到本步'

        logging.info('步骤8.点击分类')
        device.click_element(classify)
        logging.info('预期结果8.可以选择已有的分类')
        device.refresh_layout()
        device.assert_text_exist('个人')
        device.assert_text_exist('生活')
        device.assert_text_exist('工作')
        device.assert_text_exist('未分类')
        device.click_element(device.get_element_by_text('个人'))

        logging.info('步骤9.点击右上角“对勾”标识保存')
        device.click(*self.app.save_note)
        device.go_back()
        logging.info('预期结果9.保存成功，可以在选择的选择的分类中查看到保存的笔记，笔记封面展示插入的图片')
        device.click(*self.app.open_menu)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('个人'))
        device.click(*self.app.close_menu)
        device.refresh_layout()
        device.assert_text_exist('abc')
        new_note = device.get_element_by_type('ListItem', -1)
        element_area = device.get_element_area(new_note)
        pic3 = device.save_snapshot_to_local('ListItem.jpeg')
        Images.crop_picture(pic3, *element_area)
        similarity = Images.compare_image_similarity(self.app.listitem_pic, pic3)
        assert similarity > 0.8, '样式未生效'
